﻿using GDGeek;
using MrPP.Helper;
using MrPP.SocketSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MrPP.SerriedCabinet { 
    public class SerriedCabinetManager : GDGeek.Singleton<SerriedCabinetManager>
    {
        public void requestAreaColumnConfiguration() {
            var racc = new RequestAreaColumnConfiguration();
            SocketManager.Instance.sendMessage(racc);
        }

        public DataTask<RespAreaColumnConfiguration.Data> areaColumnTask() {
            DataTask<RespAreaColumnConfiguration.Data> task = new DataTask<RespAreaColumnConfiguration.Data>();
            Action<RespAreaColumnConfiguration.Data> receive = delegate (RespAreaColumnConfiguration.Data data) {
                task.setData(data);
            };
            float time = 0f;
            TaskManager.PushFront(task, delegate
            {
                RespAreaColumnConfiguration.Instance.onRecevie += receive;
                var racc = new RequestAreaColumnConfiguration();
                SocketManager.Instance.sendMessage(racc);
            });
            TaskManager.AddUpdate(task, delegate (float d)
            {
                time += d;
                if (time >= 1f) {
                    task.setData(null);
                }
            });
            TaskManager.PushBack(task, delegate
            {
                RespAreaColumnConfiguration.Instance.onRecevie -= receive;
            });

            return task;
        }

        public DataTask<RespCallback.Data> ventilate(byte[] areas)
        {
            DataTask<RespCallback.Data> task = new DataTask<RespCallback.Data>();

            Action<RespCallback.Data> receive = delegate (RespCallback.Data data) {
                task.setData(data);
            };

            float time = 0f;
            TaskManager.PushFront(task, delegate
            {
                RespCallback.Instance.onRecevie += receive;

                RequestVentilate rv = new RequestVentilate(areas);
                SocketManager.Instance.sendMessage(rv);
            });
            TaskManager.AddUpdate(task, delegate (float d)
            {
                time += d;
                if (time >= 1f)
                {
                    task.setData(null);
                }
            });
            TaskManager.PushBack(task, delegate
            {
                RespCallback.Instance.onRecevie -= receive;
            });



            return task;
        }

        public DataTask<RespCallback.Data> stop(byte[] areas)
        {
            DataTask<RespCallback.Data> task = new DataTask<RespCallback.Data>();

            Action<RespCallback.Data> receive = delegate (RespCallback.Data data) {
                task.setData(data);
            };

            float time = 0f;
            TaskManager.PushFront(task, delegate
            {
                RespCallback.Instance.onRecevie += receive;

                RequestStop rv = new RequestStop(areas);
                SocketManager.Instance.sendMessage(rv);
            });
            TaskManager.AddUpdate(task, delegate (float d)
            {
                time += d;
                if (time >= 1f)
                {
                    task.setData(null);
                }
            });
            TaskManager.PushBack(task, delegate
            {
                RespCallback.Instance.onRecevie -= receive;
            });



            return task;
        }

        public DataTask<RespCallback.Data> open(byte area, byte column)
        {
            DataTask<RespCallback.Data> task = new DataTask<RespCallback.Data>();

            Action<RespCallback.Data> receive = delegate (RespCallback.Data data) {
                task.setData(data);
            };

            float time = 0f;
            TaskManager.PushFront(task, delegate
            {
                RespCallback.Instance.onRecevie += receive;

                RequestOpen rv = new RequestOpen(area, column);
                SocketManager.Instance.sendMessage(rv);
            });
            TaskManager.AddUpdate(task, delegate (float d)
            {
                time += d;
                if (time >= 1f)
                {
                    task.setData(null);
                }
            });
            TaskManager.PushBack(task, delegate
            {
                RespCallback.Instance.onRecevie -= receive;
            });



            return task;
        }

        public DataTask<RespCallback.Data> close(byte[] areas)
        {
            DataTask<RespCallback.Data> task = new DataTask<RespCallback.Data>();

            Action<RespCallback.Data> receive = delegate (RespCallback.Data data) {
                task.setData(data);
            };

            float time = 0f;
            TaskManager.PushFront(task, delegate
            {
                RespCallback.Instance.onRecevie += receive;

                RequestClose rv = new RequestClose(areas);
                SocketManager.Instance.sendMessage(rv);
            });
            TaskManager.AddUpdate(task, delegate (float d)
            {
                time += d;
                if (time >= 1f)
                {
                    task.setData(null);
                }
            });
            TaskManager.PushBack(task, delegate
            {
                RespCallback.Instance.onRecevie -= receive;
            });



            return task;
        }

        public DataTask<RespTheInitialStatus.Data> initialStatus() {
            DataTask<RespTheInitialStatus.Data> task = new DataTask<RespTheInitialStatus.Data>();
            Action<RespTheInitialStatus.Data> receive = delegate (RespTheInitialStatus.Data data) {
                task.setData(data);
            };
            float time = 0f;
            TaskManager.PushFront(task, delegate
            {
                RespTheInitialStatus.Instance.onRecevie += receive;
                var rtis = new RequestTheInitialStatus();
                SocketManager.Instance.sendMessage(rtis);
            });
            TaskManager.AddUpdate(task, delegate (float d)
            {
                time += d;
                if (time >= 1f)
                {
                    task.setData(null);
                }
            });
            TaskManager.PushBack(task, delegate
            {
                RespTheInitialStatus.Instance.onRecevie -= receive;
            });

            return task;
        }
        public void requestTheInitialStatus() {
            var rtis = new RequestTheInitialStatus();

            SocketManager.Instance.sendMessage(rtis);

        }
    }
}